package cn.xopencode.appletweb.controller.record.roomin;

import cn.xopencode.common.framework.util.StringUtils;
import cn.xopencode.common.framework.vo.CommonResult;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.recordservice.rpc.roomin.RoomInRecordRpc;
import cn.xopencode.recordservice.rpc.roomin.dto.*;
import cn.xopencode.recordservice.rpc.roomin.vo.RoomInRecordEmpRelVO;
import cn.xopencode.recordservice.rpc.roomin.vo.RoomInRecordPageVO;
import cn.xopencode.recordservice.rpc.roomin.vo.RoomInRecordUpdateVO;
import cn.xopencode.userservice.rpc.user.UserRpc;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "配电室进出记录 API")
@RestController
@RequestMapping("/roominrecord")
@Validated
public class RoomInRecordController {

    @DubboReference(version = "${dubbo.consumer.RoomInRecordRpc.version}")
    private RoomInRecordRpc roomInRecordRpc;

    @DubboReference(version = "${dubbo.consumer.UserRpc.version}")
    private UserRpc userRpc;

    /**
     * 根据查询条件 分页获取所有的配电室进出记录
     * @param pageDTO
     * @return
     */
    @PostMapping("/getAllByQuery")
    @ApiOperation("根据查询条件分页获取所有的配电室出入记录")
    public CommonResult<PageResult<RoomInRecordPageVO>> getPageRoomInRecord(RoomInRecordPageDTO pageDTO){
        CommonResult<PageResult<RoomInRecordPageVO>> result = roomInRecordRpc.pageRoomInRecord(pageDTO);
        List<RoomInRecordPageVO> list = result.getData().getList();
        list.forEach(pageVO -> {
            String empIds = pageVO.getEmpIds();
            if(!StringUtils.isEmpty(empIds)){
                pageVO.setEmpRels(userRpc.getEmployeeName(empIds));
            }
        });
        return result;
    }

    /**
     * 新增 配电室出入记录
     * @param createDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增配电室出入记录")
    public CommonResult<Integer> createRoomInRecord(@Valid RoomInRecordCreateDTO createDTO){
        return roomInRecordRpc.createRoomInRecord(createDTO);
    }

    /**
     * 更新 配电室出入记录
     * @param updateDTO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("更新配电室出入记录")
    public CommonResult<Integer> updateRoomInRecord(@Valid RoomInRecordUpdateDTO updateDTO){
        return roomInRecordRpc.updateRoomInRecord(updateDTO);
    }

    /**
     * 根据id获取配电室进出记录详细
     * @param roomId
     * @return
     */
    @GetMapping("/getById")
    @ApiOperation("根据id获取配电室进出记录详细")
    public CommonResult<RoomInRecordUpdateVO> getRoomInRecordById(String roomId){
        CommonResult<RoomInRecordUpdateVO> result = roomInRecordRpc.getRoomInRecordById(roomId);
        RoomInRecordUpdateVO updateVO = result.getData();
        if(updateVO != null){
            List<String> list = updateVO.getEmpList().stream().map(RoomInRecordEmpRelVO::getEmpId).collect(Collectors.toList());
            String empNames =  userRpc.getEmployeeName(StringUtils.join(list, ","));
            updateVO.setEmpNames(empNames);
        }
        return result;
    }


    /**
     * 后台--根据查询条件分页获取所有的配电室出入记录
     * @param pageDTO
     * @return
     */
    @PostMapping("/getAllAdminByQuery")
    @ApiOperation("后台--根据查询条件分页获取所有的配电室出入记录")
    public CommonResult<PageResult<AdminRoomInRecordVO>> getAdminPageRoomInRecord(AdminRoomInRecordDTO pageDTO) {
        CommonResult<PageResult<AdminRoomInRecordVO>> result = roomInRecordRpc.getAdminPageRoomInRecord(pageDTO);
        List<AdminRoomInRecordVO> list = result.getData().getList();
        list.forEach(pageVO -> {
            if (!StringUtils.isEmpty(pageVO.getEmpId()))
                pageVO.setEmpName(userRpc.getEmployeeName(pageVO.getEmpId()));
        });
        return result;
    }

}
